package com.feijia.aijingjingus;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.style.ForegroundColorSpan;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.DayViewDecorator;
import com.prolificinteractive.materialcalendarview.DayViewFacade;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static com.feijia.aijingjingus.GlobalHelper.DaysBeforeOvulation;
import static com.feijia.aijingjingus.GlobalHelper.FirstDay;
import static com.feijia.aijingjingus.GlobalHelper.Menstruation;
import static com.feijia.aijingjingus.GlobalHelper.ONE_DAY_MS;
import static com.feijia.aijingjingus.GlobalHelper.OvulationLength;

/**
 * Created by feiga on 2017/8/11.
 */

public class MCVOvulationDecorator implements DayViewDecorator {
    private final Context context;

    private int ABSofOvulation;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    public MCVOvulationDecorator( Context context,int ABSofOvulation) {
        this.context =context;
        this.ABSofOvulation = ABSofOvulation;
    }
    @Override
    public boolean shouldDecorate(CalendarDay day) {
        try {
            long nowDay = (day.getCalendar().getTimeInMillis()/ ONE_DAY_MS)%Menstruation;

            Date date = sdf.parse(FirstDay);
            Calendar cl = Calendar.getInstance();
            cl.setTime(date);
            long firstDay =(cl.getTimeInMillis()/ ONE_DAY_MS)%Menstruation;
            long firstDayOfOvulation =DaysBeforeOvulation;
            nowDay -=firstDay;
            if(nowDay<0)
                nowDay+=Menstruation;
            int days=(int)((nowDay-firstDayOfOvulation))+1;
            return (Math.abs(days-OvulationLength/2)==ABSofOvulation)&&days!=0;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void decorate(DayViewFacade view) {
        view.addSpan(new ForegroundColorSpan(Color.WHITE));
        //view.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.calendarview_pink_big));
        Drawable d = context.getResources().getDrawable(R.drawable.heart6);
        /*int Alpha = 90-ABSofOvulation*15;
        Alpha = (int)(Alpha/100f*255);
        d.mutate().setAlpha(Alpha);*/

        if(ABSofOvulation==0)
        {
            d = context.getResources().getDrawable(R.drawable.heart1);
        }
        else if(ABSofOvulation==1)
        {
            d = context.getResources().getDrawable(R.drawable.heart2);
        }
        else if(ABSofOvulation==2)
        {
            d = context.getResources().getDrawable(R.drawable.heart3);
        }
        else if(ABSofOvulation==3)
        {
            d = context.getResources().getDrawable(R.drawable.heart4);
        }
        else if(ABSofOvulation==4)
        {
            d = context.getResources().getDrawable(R.drawable.heart5);
        }
        else if(ABSofOvulation==5)
        {
            d = context.getResources().getDrawable(R.drawable.heart6);
        }
        view.setBackgroundDrawable(d);

        System.out.println("Alpha B:"+d.getAlpha());
    }
}
